package com.nexus.service.sys.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nexus.core.exception.ServiceException;
import com.nexus.core.service.BaseService;
import com.nexus.core.sys.entity.SysUserRole;
import com.nexus.core.sys.dto.SysRoleDTO;
import com.nexus.core.sys.dto.SysUserRoleDTO;
import com.nexus.service.sys.mapper.SysUserRoleMapper;
import com.nexus.service.sys.service.mapstruct.SysUserRoleWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户角色Service
 *
 * @author 路正宁
 * @version 2023-03-16
 */
//@DS("zxhy_platform_db")
//@Service
@Transactional(rollbackFor = {ServiceException.class,Exception.class, RuntimeException.class})
public class SysUserRoleService extends BaseService<SysUserRoleMapper, SysUserRole,SysUserRoleDTO,SysUserRoleWrapper> {

    @Autowired
    SysUserRoleWrapper sysUserRoleWrapperImpl;
    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public SysUserRoleDTO findById(String id) {
        return baseMapper.findById(id);
    }

    /**
     * 自定义分页检索
     *
     * @param page
     * @param queryWrapper
     * @return
     */
    public IPage<SysUserRoleDTO> findPage(Page<SysUserRoleDTO> page, QueryWrapper queryWrapper) {
        queryWrapper.eq("a.deleted", 0); // 排除已经删除
        return baseMapper.findList(page, queryWrapper);
    }

    /**
     * 根据用户Id查询角色
     * @author luzhengning
     * @date 2023/4/10 13:43
     */
    // public List<SysRoleDTO> findByUserId(String userId){
    //     QueryWrapper queryWrapper = new QueryWrapper();
    //     queryWrapper.eq("a.sys_user", userId);
    //     queryWrapper.eq("a.deleted", "0");
    //     queryWrapper.orderByDesc("a.create_time");
    //     //查询用户角色
    //     IPage<SysUserRoleDTO> page = baseMapper.findList(new Page<>(1, 1000), queryWrapper);
    //     if (ObjectUtils.isEmpty(page) || ObjectUtils.isEmpty(page.getRecords())) {
    //         return null;
    //     }
    //     List<SysUserRoleDTO> records = page.getRecords();
    //     //提取角色ID
    //     List<SysRoleDTO> roles = records.stream().map(SysUserRoleDTO::getRole).collect(Collectors.toList());
    //     return roles;
    // }


    /**
     * 删除角色关联
     * @Date 2023/9/6 10:05
     * @Author luzhengning
     **/
    public boolean deleteByRoleIds(List<String> roleIds){
        if(ObjectUtils.isEmpty(roleIds))return false;
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.in("sys_role",roleIds);
        return this.remove(queryWrapper);
    }

    public boolean deleteByUserAndRole(String userId,String roleId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("sys_user", userId);
        queryWrapper.eq("sys_role", roleId);
        return remove(queryWrapper);
    }

    public List<SysUserRoleDTO> findUserAndRole(String userId,String roleId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("a.sys_user", userId);
        queryWrapper.eq("a.sys_role", roleId);
        queryWrapper.eq("a.deleted", "0");
        //查询用户角色
        IPage<SysUserRoleDTO> page = baseMapper.findList(new Page<>(1, 1000), queryWrapper);
        return page.getRecords();
    }

}
